<?php
/* FILE: process_create_collection.php
 * DESCRIPTION: Process called when a user presses the Create Collection button in new_collection.php
 * POST DATA: rating, description, col_name, attribute data, attributes types
 * GET DATA: N/A
 */ 
 
// Inialize session
session_start();

// Include database connection settings
include('config.inc');

// Include functions
include('includes/functions.php');

$success = true;
$stopper = 0;
$collectionName = trim((string)$_POST['col_name']);

$attributes = array(
	trim((string)$_POST['attr1']),
	trim((string)$_POST['attr2']),
	trim((string)$_POST['attr3']),
	trim((string)$_POST['attr4']),
	trim((string)$_POST['attr5']),
	trim((string)$_POST['attr6']),
	trim((string)$_POST['attr7']),
	trim((string)$_POST['attr8']),
	trim((string)$_POST['attr9']),
	trim((string)$_POST['attr10'])
);
$types = array(
	trim((string)$_POST['type1']),
	trim((string)$_POST['type2']),
	trim((string)$_POST['type3']),
	trim((string)$_POST['type4']),
	trim((string)$_POST['type5']),
	trim((string)$_POST['type6']),
	trim((string)$_POST['type7']),
	trim((string)$_POST['type8']),
	trim((string)$_POST['type9']),
	trim((string)$_POST['type10'])
);

switch((string)$_POST['rating'])
{
	case 'one': $rating = 1; break;
	case 'two': $rating = 2; break;
	case 'thr': $rating = 3; break;
	case 'four': $rating = 4; break;
	case 'five': $rating = 5; break;
	default: $rating = 0; break;
}

if(($success = checkValidCollection($collectionName, $attributes, true)) == 1)
{	
	//If no problems, create table
	$insertCollection = 
		"INSERT INTO collection (collection_name, collection_desc, collection_rating, collection_user_id) VALUES ('"
		. mysql_real_escape_string($_SESSION['user_name']) 
		. "_" 
		. mysql_real_escape_string($collectionName) 
		. "', '" 
		. mysql_real_escape_string($_POST['col_desc']) 
		. "', '"
		. $rating
		. "', '"
		. mysql_real_escape_string($_SESSION['user_name']) 
		. "');";
	if(!mysql_query($insertCollection))
	{
		die($insertCollection);
	}
	
	$collectionName = str_replace(" ", "_", $collectionName);

	$user = (string)$_SESSION['user_name'];
	$tableName = $user."_".$collectionName;
	$tableName = str_replace(".", "_", $tableName);
	$tableQuery = "CREATE TABLE " . ($tableName) . "(" . ($collectionName) . "_item_id INTEGER NOT NULL PRIMARY KEY,";
	
	$numAttributes = getOccupiedRowCount($attributes);
	$numAttributesAdded = 1;
	for($i = 0; $i < count($attributes);$i++)
	{		
		if(!empty($attributes[$i]))
		{
			switch($types[$i])
			{
				case "int": $curType = "INTEGER"; break;
				case "dbl": $curType = "REAL"; break;
				default: $curType = "VARCHAR(128)"; break;
			}
			
			//Print comma unless last attribute.
			if($numAttributesAdded != $numAttributes)
			{		
				$tableQuery = $tableQuery 
							. ($attributes[$i]) 
							. " " 
							. ($curType) 
							. ",";
				
			}
			else
			{
				$tableQuery = $tableQuery 
							. ($attributes[$i]) 
							. " " 
							. ($curType) 
							. ", FOREIGN KEY (" 
							. $_POST['col_name'] 
							. "_item_id) REFERENCES item(item_id) ON DELETE CASCADE" 
							. ");";
			}
			$numAttributesAdded++;
			$insertAttrQuery = 
				"INSERT INTO attribute (attribute_name, attribute_type, attribute_collection_name, attribute_user_id) VALUES ('"
				. ($attributes[$i]) . "', '"
				. ($curType) . "', '"
				. mysql_real_escape_string($_SESSION['user_name'])
				. "_"
				. mysql_real_escape_string($collectionName) . "', '"
				. mysql_real_escape_string($_SESSION['user_name']) . "');";
			
			if(!mysql_query($insertAttrQuery))
			{
				die($insertAttrQuery);
			}
		}
	}
	
	// Executes query and enters condition is fails.
	if(!mysql_query($tableQuery))
	{
		die($tableQuery 
		. ": An error has occurred creating your collections attributes! Please contact us here.");
	}
	
	header('Location: new_collection.php?err=success');
}
elseif($success == -1)
{
	header('Location: new_collection.php?err=invalcolname');
}
elseif($success == -2)
{
	header('Location: new_collection.php?err=invalattsname');
}
elseif($success == -3)
{
	header('Location: new_collection.php?err=dupattsname');
}
elseif($success == -4)
{
	header('Location: new_collection.php?err=nullatts');
}
else
{
	header('Location: new_collection.php?err=unknown');
}
?>